package com.scopionstudio.utils;

import java.util.ArrayList;
import java.util.List;

import com.scopionstudio.bean.DescriptionSongs;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;

public class DatabaseHandler extends SQLiteOpenHelper{


	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "songsManager";
	private static final String TABLE_NAME = "songsDetails";
	
	// Declare name of column
	private static final String KEY_ID = "id";
	private static final String KEY_TITLE = "title";
	private static final String KEY_SINGER = "singer";
	private static final String KEY_LINKS = "link";
	
	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}
	
	

	@Override
	public void onCreate(SQLiteDatabase db) {
		String 	CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME + " (" + KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_TITLE +
				" TEXT," + KEY_SINGER + " TEXT," + KEY_LINKS + " TEXT )";
		db.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String CHECK_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(CHECK_TABLE);
		onCreate(db);
	}
	// Add new Contact
	public void addNewContact(DescriptionSongs mContacts){
		SQLiteDatabase mDatabase = this.getWritableDatabase();
		ContentValues mValues = new ContentValues();
		mValues.put(KEY_TITLE	, mContacts.getName());
		mValues.put(KEY_SINGER, mContacts.getSinger());
		mValues.put(KEY_LINKS , mContacts.getLink());
		mDatabase.insert(TABLE_NAME, null, mValues);
		mDatabase.close();
	}
	
	//Getting single contact
	public DescriptionSongs getContactByID(int id){
		SQLiteDatabase mDatabase = this.getReadableDatabase();
		
		Cursor mCursor = mDatabase.query(TABLE_NAME	, new String[]{KEY_ID,KEY_TITLE,KEY_SINGER,KEY_LINKS}, KEY_ID +" = ?", 
				new String[]{String.valueOf(id)}, null, null, null);
		if(mCursor !=null)
			mCursor.moveToFirst();
			DescriptionSongs mSongs = new DescriptionSongs(Integer.parseInt(mCursor.getString(0)), 
						mCursor.getString(1), mCursor.getString(2), mCursor.getString(3),  mCursor.getString(4));
		return mSongs;
	}
	//get maxcolumm
	public int getMaxID(){
		SQLiteDatabase mDatabase = this.getReadableDatabase();
		final SQLiteStatement stmt = mDatabase
	            .compileStatement("SELECT MAX(id) FROM "+TABLE_NAME);
		 /*try { a.moveToFirst(); return a.getInt(0); } finally { a.close(); } */
		return (int) stmt.simpleQueryForLong();
	}
	
	// Get all contact
	public List<DescriptionSongs> getAllContact(){
		List<DescriptionSongs> mDescriptionSongs = new ArrayList<DescriptionSongs>();
		//Query select all
		String SQL_SELECT_ALL = "SELECT * FROM "+ TABLE_NAME;
		SQLiteDatabase mDatabase = this.getWritableDatabase();
		Cursor mCursor = mDatabase.rawQuery(SQL_SELECT_ALL, null);
		if(mCursor != null){
			if(mCursor.moveToFirst()){
				do {
					DescriptionSongs mSongs = new DescriptionSongs();
					mSongs.setId(Integer.parseInt(mCursor.getString(0)));
					mSongs.setName(mCursor.getString(1));
					mSongs.setSinger(mCursor.getString(2));
					mSongs.setLink(mCursor.getString(3));
					mDescriptionSongs.add(mSongs);
				} while (mCursor.moveToNext());
			}
		}
		mDatabase.close();
		return mDescriptionSongs;
	}
	//Delete single contact
	public void deleteContact(long Id){
		SQLiteDatabase mDatabase = this.getWritableDatabase();
		mDatabase.delete(TABLE_NAME, KEY_ID + " = " + Id, null);
		mDatabase.close();
	}
	
	// Getting contact count
	public int getCountContact(){
		SQLiteDatabase mDatabase = this.getWritableDatabase();
		String QUERY_GETALL = "SELECT * FROM " + TABLE_NAME;
		Cursor mCursor = mDatabase.rawQuery(QUERY_GETALL, null);
		mCursor.close();
		mDatabase.close();
		return mCursor.getCount();
	}
	public void deleteAllContact(){
		SQLiteDatabase mDatabase = this.getWritableDatabase();
		//String QUERY_GETALL = "DELETE FROM " + TABLE_NAME;
		mDatabase.delete(TABLE_NAME, null, null);
		mDatabase.close();
	}

}
















